{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 8.2 量化金融 - 股票数据获取"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 8.2.1 股票基本数据获取"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "这里介绍一个免费的财经数据Python接口包：Tushare库，通过它我们能够免费地调用历史行情数据来进行分析。其官方地址为：http://tushare.org/\n",
    "如果是想查看股价行情数据，可以访问相应网址：http://tushare.org/trading.html\n",
    "\n",
    "1.Tushare库的基本介绍\n",
    "\n",
    "推荐通过PIP安装法来安装Tushare库，以Windows系统为例，具体方法是：通过Win + R组合键调出运行框，输入cmd后回车，然后在弹出框中输入pip install tushare后按一下Enter回车键的方法来进行安装。如果在1.2.3节讲到的Jupyter Notebook编辑器中安装的话，只需要在代码框中输入!pip instll tushare（注意是英文格式下的!）然后运行该行代码框即可。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "(1) 获得日线行情数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>open</th>\n",
       "      <th>high</th>\n",
       "      <th>close</th>\n",
       "      <th>low</th>\n",
       "      <th>volume</th>\n",
       "      <th>price_change</th>\n",
       "      <th>p_change</th>\n",
       "      <th>ma5</th>\n",
       "      <th>ma10</th>\n",
       "      <th>ma20</th>\n",
       "      <th>v_ma5</th>\n",
       "      <th>v_ma10</th>\n",
       "      <th>v_ma20</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2019-01-31</th>\n",
       "      <td>27.39</td>\n",
       "      <td>28.15</td>\n",
       "      <td>27.75</td>\n",
       "      <td>27.00</td>\n",
       "      <td>411857.59</td>\n",
       "      <td>0.54</td>\n",
       "      <td>1.99</td>\n",
       "      <td>26.800</td>\n",
       "      <td>26.153</td>\n",
       "      <td>25.641</td>\n",
       "      <td>426579.02</td>\n",
       "      <td>351523.31</td>\n",
       "      <td>320269.20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-01-30</th>\n",
       "      <td>26.70</td>\n",
       "      <td>27.82</td>\n",
       "      <td>27.21</td>\n",
       "      <td>26.63</td>\n",
       "      <td>592303.19</td>\n",
       "      <td>0.33</td>\n",
       "      <td>1.23</td>\n",
       "      <td>26.332</td>\n",
       "      <td>25.875</td>\n",
       "      <td>25.457</td>\n",
       "      <td>391193.72</td>\n",
       "      <td>334927.14</td>\n",
       "      <td>310794.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-01-29</th>\n",
       "      <td>25.91</td>\n",
       "      <td>26.88</td>\n",
       "      <td>26.88</td>\n",
       "      <td>25.87</td>\n",
       "      <td>368071.62</td>\n",
       "      <td>0.82</td>\n",
       "      <td>3.15</td>\n",
       "      <td>25.952</td>\n",
       "      <td>25.696</td>\n",
       "      <td>25.292</td>\n",
       "      <td>302102.48</td>\n",
       "      <td>302443.43</td>\n",
       "      <td>293529.36</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-01-28</th>\n",
       "      <td>26.20</td>\n",
       "      <td>26.62</td>\n",
       "      <td>26.06</td>\n",
       "      <td>25.86</td>\n",
       "      <td>308906.56</td>\n",
       "      <td>-0.04</td>\n",
       "      <td>-0.15</td>\n",
       "      <td>25.656</td>\n",
       "      <td>25.524</td>\n",
       "      <td>25.139</td>\n",
       "      <td>304355.52</td>\n",
       "      <td>302512.15</td>\n",
       "      <td>291266.32</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-01-25</th>\n",
       "      <td>25.51</td>\n",
       "      <td>26.35</td>\n",
       "      <td>26.10</td>\n",
       "      <td>25.49</td>\n",
       "      <td>451756.16</td>\n",
       "      <td>0.69</td>\n",
       "      <td>2.71</td>\n",
       "      <td>25.574</td>\n",
       "      <td>25.420</td>\n",
       "      <td>25.008</td>\n",
       "      <td>293674.18</td>\n",
       "      <td>289949.63</td>\n",
       "      <td>293446.08</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "             open   high  close    low     volume  price_change  p_change  \\\n",
       "date                                                                        \n",
       "2019-01-31  27.39  28.15  27.75  27.00  411857.59          0.54      1.99   \n",
       "2019-01-30  26.70  27.82  27.21  26.63  592303.19          0.33      1.23   \n",
       "2019-01-29  25.91  26.88  26.88  25.87  368071.62          0.82      3.15   \n",
       "2019-01-28  26.20  26.62  26.06  25.86  308906.56         -0.04     -0.15   \n",
       "2019-01-25  25.51  26.35  26.10  25.49  451756.16          0.69      2.71   \n",
       "\n",
       "               ma5    ma10    ma20      v_ma5     v_ma10     v_ma20  \n",
       "date                                                                 \n",
       "2019-01-31  26.800  26.153  25.641  426579.02  351523.31  320269.20  \n",
       "2019-01-30  26.332  25.875  25.457  391193.72  334927.14  310794.00  \n",
       "2019-01-29  25.952  25.696  25.292  302102.48  302443.43  293529.36  \n",
       "2019-01-28  25.656  25.524  25.139  304355.52  302512.15  291266.32  \n",
       "2019-01-25  25.574  25.420  25.008  293674.18  289949.63  293446.08  "
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import tushare as ts\n",
    "df = ts.get_hist_data('000002', start='2018-01-01', end='2019-01-31')\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "注意，如果不写开始及结束日期，直接写ts.get_hist_data('000002')会默认调取从当天往前3年的数据。此外，上面代码也可以简写成："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>open</th>\n",
       "      <th>high</th>\n",
       "      <th>close</th>\n",
       "      <th>low</th>\n",
       "      <th>volume</th>\n",
       "      <th>price_change</th>\n",
       "      <th>p_change</th>\n",
       "      <th>ma5</th>\n",
       "      <th>ma10</th>\n",
       "      <th>ma20</th>\n",
       "      <th>v_ma5</th>\n",
       "      <th>v_ma10</th>\n",
       "      <th>v_ma20</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2019-01-31</th>\n",
       "      <td>27.39</td>\n",
       "      <td>28.15</td>\n",
       "      <td>27.75</td>\n",
       "      <td>27.00</td>\n",
       "      <td>411857.59</td>\n",
       "      <td>0.54</td>\n",
       "      <td>1.99</td>\n",
       "      <td>26.800</td>\n",
       "      <td>26.153</td>\n",
       "      <td>25.641</td>\n",
       "      <td>426579.02</td>\n",
       "      <td>351523.31</td>\n",
       "      <td>320269.20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-01-30</th>\n",
       "      <td>26.70</td>\n",
       "      <td>27.82</td>\n",
       "      <td>27.21</td>\n",
       "      <td>26.63</td>\n",
       "      <td>592303.19</td>\n",
       "      <td>0.33</td>\n",
       "      <td>1.23</td>\n",
       "      <td>26.332</td>\n",
       "      <td>25.875</td>\n",
       "      <td>25.457</td>\n",
       "      <td>391193.72</td>\n",
       "      <td>334927.14</td>\n",
       "      <td>310794.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-01-29</th>\n",
       "      <td>25.91</td>\n",
       "      <td>26.88</td>\n",
       "      <td>26.88</td>\n",
       "      <td>25.87</td>\n",
       "      <td>368071.62</td>\n",
       "      <td>0.82</td>\n",
       "      <td>3.15</td>\n",
       "      <td>25.952</td>\n",
       "      <td>25.696</td>\n",
       "      <td>25.292</td>\n",
       "      <td>302102.48</td>\n",
       "      <td>302443.43</td>\n",
       "      <td>293529.36</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-01-28</th>\n",
       "      <td>26.20</td>\n",
       "      <td>26.62</td>\n",
       "      <td>26.06</td>\n",
       "      <td>25.86</td>\n",
       "      <td>308906.56</td>\n",
       "      <td>-0.04</td>\n",
       "      <td>-0.15</td>\n",
       "      <td>25.656</td>\n",
       "      <td>25.524</td>\n",
       "      <td>25.139</td>\n",
       "      <td>304355.52</td>\n",
       "      <td>302512.15</td>\n",
       "      <td>291266.32</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-01-25</th>\n",
       "      <td>25.51</td>\n",
       "      <td>26.35</td>\n",
       "      <td>26.10</td>\n",
       "      <td>25.49</td>\n",
       "      <td>451756.16</td>\n",
       "      <td>0.69</td>\n",
       "      <td>2.71</td>\n",
       "      <td>25.574</td>\n",
       "      <td>25.420</td>\n",
       "      <td>25.008</td>\n",
       "      <td>293674.18</td>\n",
       "      <td>289949.63</td>\n",
       "      <td>293446.08</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "             open   high  close    low     volume  price_change  p_change  \\\n",
       "date                                                                        \n",
       "2019-01-31  27.39  28.15  27.75  27.00  411857.59          0.54      1.99   \n",
       "2019-01-30  26.70  27.82  27.21  26.63  592303.19          0.33      1.23   \n",
       "2019-01-29  25.91  26.88  26.88  25.87  368071.62          0.82      3.15   \n",
       "2019-01-28  26.20  26.62  26.06  25.86  308906.56         -0.04     -0.15   \n",
       "2019-01-25  25.51  26.35  26.10  25.49  451756.16          0.69      2.71   \n",
       "\n",
       "               ma5    ma10    ma20      v_ma5     v_ma10     v_ma20  \n",
       "date                                                                 \n",
       "2019-01-31  26.800  26.153  25.641  426579.02  351523.31  320269.20  \n",
       "2019-01-30  26.332  25.875  25.457  391193.72  334927.14  310794.00  \n",
       "2019-01-29  25.952  25.696  25.292  302102.48  302443.43  293529.36  \n",
       "2019-01-28  25.656  25.524  25.139  304355.52  302512.15  291266.32  \n",
       "2019-01-25  25.574  25.420  25.008  293674.18  289949.63  293446.08  "
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = ts.get_hist_data('000002','2018-01-01', '2019-01-31')\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**补充知识点：get_k_data()函数**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "因为get_hist_data()函数不仅获得了股票的基本价格信息，还获取了价格变化、均线价格等衍生变量，所以它最多也只能调取当天往前3年的数据，如果想调取超过3年的日线级别数据，得用ts.get_k_data()函数，它只获取股价的基本数据，代码如下："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>date</th>\n",
       "      <th>open</th>\n",
       "      <th>close</th>\n",
       "      <th>high</th>\n",
       "      <th>low</th>\n",
       "      <th>volume</th>\n",
       "      <th>code</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2000-01-04</td>\n",
       "      <td>0.584</td>\n",
       "      <td>0.614</td>\n",
       "      <td>0.620</td>\n",
       "      <td>0.572</td>\n",
       "      <td>45747.08</td>\n",
       "      <td>000002</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2000-01-05</td>\n",
       "      <td>0.617</td>\n",
       "      <td>0.599</td>\n",
       "      <td>0.623</td>\n",
       "      <td>0.596</td>\n",
       "      <td>46136.73</td>\n",
       "      <td>000002</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2000-01-06</td>\n",
       "      <td>0.596</td>\n",
       "      <td>0.627</td>\n",
       "      <td>0.632</td>\n",
       "      <td>0.587</td>\n",
       "      <td>71920.31</td>\n",
       "      <td>000002</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2000-01-07</td>\n",
       "      <td>0.631</td>\n",
       "      <td>0.655</td>\n",
       "      <td>0.656</td>\n",
       "      <td>0.624</td>\n",
       "      <td>136349.36</td>\n",
       "      <td>000002</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2000-01-10</td>\n",
       "      <td>0.673</td>\n",
       "      <td>0.721</td>\n",
       "      <td>0.721</td>\n",
       "      <td>0.665</td>\n",
       "      <td>142424.86</td>\n",
       "      <td>000002</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         date   open  close   high    low     volume    code\n",
       "0  2000-01-04  0.584  0.614  0.620  0.572   45747.08  000002\n",
       "1  2000-01-05  0.617  0.599  0.623  0.596   46136.73  000002\n",
       "2  2000-01-06  0.596  0.627  0.632  0.587   71920.31  000002\n",
       "3  2000-01-07  0.631  0.655  0.656  0.624  136349.36  000002\n",
       "4  2000-01-10  0.673  0.721  0.721  0.665  142424.86  000002"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = ts.get_k_data('000002', start='2000-01-01', end='2019-01-31')\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "通过get_k_data()函数获取的数据没有像get_hist_data()函数那样将日期默认设为行索引，这里的日期还是作为一个普通的列（date列），如果想把这里的date列转为行索引，可以使用设置索引的set_index()函数，代码如下："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>open</th>\n",
       "      <th>close</th>\n",
       "      <th>high</th>\n",
       "      <th>low</th>\n",
       "      <th>volume</th>\n",
       "      <th>code</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2000-01-04</th>\n",
       "      <td>0.584</td>\n",
       "      <td>0.614</td>\n",
       "      <td>0.620</td>\n",
       "      <td>0.572</td>\n",
       "      <td>45747.08</td>\n",
       "      <td>000002</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2000-01-05</th>\n",
       "      <td>0.617</td>\n",
       "      <td>0.599</td>\n",
       "      <td>0.623</td>\n",
       "      <td>0.596</td>\n",
       "      <td>46136.73</td>\n",
       "      <td>000002</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2000-01-06</th>\n",
       "      <td>0.596</td>\n",
       "      <td>0.627</td>\n",
       "      <td>0.632</td>\n",
       "      <td>0.587</td>\n",
       "      <td>71920.31</td>\n",
       "      <td>000002</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2000-01-07</th>\n",
       "      <td>0.631</td>\n",
       "      <td>0.655</td>\n",
       "      <td>0.656</td>\n",
       "      <td>0.624</td>\n",
       "      <td>136349.36</td>\n",
       "      <td>000002</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2000-01-10</th>\n",
       "      <td>0.673</td>\n",
       "      <td>0.721</td>\n",
       "      <td>0.721</td>\n",
       "      <td>0.665</td>\n",
       "      <td>142424.86</td>\n",
       "      <td>000002</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "             open  close   high    low     volume    code\n",
       "date                                                     \n",
       "2000-01-04  0.584  0.614  0.620  0.572   45747.08  000002\n",
       "2000-01-05  0.617  0.599  0.623  0.596   46136.73  000002\n",
       "2000-01-06  0.596  0.627  0.632  0.587   71920.31  000002\n",
       "2000-01-07  0.631  0.655  0.656  0.624  136349.36  000002\n",
       "2000-01-10  0.673  0.721  0.721  0.665  142424.86  000002"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = df.set_index('date')  # 或者写成：df.set_index('date', inplace=True)\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "(2) 获得分钟级别的数据"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "通过设置ktype参数可以获得分钟级别的数据，代码如下："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>open</th>\n",
       "      <th>high</th>\n",
       "      <th>close</th>\n",
       "      <th>low</th>\n",
       "      <th>volume</th>\n",
       "      <th>price_change</th>\n",
       "      <th>p_change</th>\n",
       "      <th>ma5</th>\n",
       "      <th>ma10</th>\n",
       "      <th>ma20</th>\n",
       "      <th>v_ma5</th>\n",
       "      <th>v_ma10</th>\n",
       "      <th>v_ma20</th>\n",
       "      <th>turnover</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2020-01-03 15:00:00</th>\n",
       "      <td>32.06</td>\n",
       "      <td>32.07</td>\n",
       "      <td>32.06</td>\n",
       "      <td>32.05</td>\n",
       "      <td>3920.32</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>32.122</td>\n",
       "      <td>32.113</td>\n",
       "      <td>32.0350</td>\n",
       "      <td>15322.7</td>\n",
       "      <td>17669.5</td>\n",
       "      <td>13041.0</td>\n",
       "      <td>0.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-03 14:55:00</th>\n",
       "      <td>32.11</td>\n",
       "      <td>32.11</td>\n",
       "      <td>32.07</td>\n",
       "      <td>32.03</td>\n",
       "      <td>8377.52</td>\n",
       "      <td>-0.04</td>\n",
       "      <td>-0.12</td>\n",
       "      <td>32.136</td>\n",
       "      <td>32.103</td>\n",
       "      <td>32.0290</td>\n",
       "      <td>19359.3</td>\n",
       "      <td>17817.5</td>\n",
       "      <td>13428.9</td>\n",
       "      <td>0.01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-03 14:50:00</th>\n",
       "      <td>32.20</td>\n",
       "      <td>32.21</td>\n",
       "      <td>32.12</td>\n",
       "      <td>32.11</td>\n",
       "      <td>13402.00</td>\n",
       "      <td>-0.08</td>\n",
       "      <td>-0.25</td>\n",
       "      <td>32.154</td>\n",
       "      <td>32.093</td>\n",
       "      <td>32.0175</td>\n",
       "      <td>23136.3</td>\n",
       "      <td>17962.0</td>\n",
       "      <td>13959.7</td>\n",
       "      <td>0.01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-03 14:45:00</th>\n",
       "      <td>32.16</td>\n",
       "      <td>32.21</td>\n",
       "      <td>32.20</td>\n",
       "      <td>32.12</td>\n",
       "      <td>24470.90</td>\n",
       "      <td>0.04</td>\n",
       "      <td>0.12</td>\n",
       "      <td>32.160</td>\n",
       "      <td>32.078</td>\n",
       "      <td>32.0050</td>\n",
       "      <td>24442.3</td>\n",
       "      <td>17137.9</td>\n",
       "      <td>13903.3</td>\n",
       "      <td>0.03</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-03 14:40:00</th>\n",
       "      <td>32.13</td>\n",
       "      <td>32.18</td>\n",
       "      <td>32.16</td>\n",
       "      <td>32.13</td>\n",
       "      <td>26443.00</td>\n",
       "      <td>0.03</td>\n",
       "      <td>0.09</td>\n",
       "      <td>32.132</td>\n",
       "      <td>32.056</td>\n",
       "      <td>31.9880</td>\n",
       "      <td>23976.3</td>\n",
       "      <td>15128.1</td>\n",
       "      <td>13491.1</td>\n",
       "      <td>0.03</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                      open   high  close    low    volume  price_change  \\\n",
       "date                                                                      \n",
       "2020-01-03 15:00:00  32.06  32.07  32.06  32.05   3920.32          0.00   \n",
       "2020-01-03 14:55:00  32.11  32.11  32.07  32.03   8377.52         -0.04   \n",
       "2020-01-03 14:50:00  32.20  32.21  32.12  32.11  13402.00         -0.08   \n",
       "2020-01-03 14:45:00  32.16  32.21  32.20  32.12  24470.90          0.04   \n",
       "2020-01-03 14:40:00  32.13  32.18  32.16  32.13  26443.00          0.03   \n",
       "\n",
       "                     p_change     ma5    ma10     ma20    v_ma5   v_ma10  \\\n",
       "date                                                                       \n",
       "2020-01-03 15:00:00      0.00  32.122  32.113  32.0350  15322.7  17669.5   \n",
       "2020-01-03 14:55:00     -0.12  32.136  32.103  32.0290  19359.3  17817.5   \n",
       "2020-01-03 14:50:00     -0.25  32.154  32.093  32.0175  23136.3  17962.0   \n",
       "2020-01-03 14:45:00      0.12  32.160  32.078  32.0050  24442.3  17137.9   \n",
       "2020-01-03 14:40:00      0.09  32.132  32.056  31.9880  23976.3  15128.1   \n",
       "\n",
       "                      v_ma20  turnover  \n",
       "date                                    \n",
       "2020-01-03 15:00:00  13041.0      0.00  \n",
       "2020-01-03 14:55:00  13428.9      0.01  \n",
       "2020-01-03 14:50:00  13959.7      0.01  \n",
       "2020-01-03 14:45:00  13903.3      0.03  \n",
       "2020-01-03 14:40:00  13491.1      0.03  "
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = ts.get_hist_data('000002', ktype='5')\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "(3) 获得实时行情数据\n",
    "\n",
    "通过如下代码可以实时取得股票当前报价和成交信息："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>open</th>\n",
       "      <th>pre_close</th>\n",
       "      <th>price</th>\n",
       "      <th>high</th>\n",
       "      <th>low</th>\n",
       "      <th>bid</th>\n",
       "      <th>ask</th>\n",
       "      <th>volume</th>\n",
       "      <th>amount</th>\n",
       "      <th>...</th>\n",
       "      <th>a2_p</th>\n",
       "      <th>a3_v</th>\n",
       "      <th>a3_p</th>\n",
       "      <th>a4_v</th>\n",
       "      <th>a4_p</th>\n",
       "      <th>a5_v</th>\n",
       "      <th>a5_p</th>\n",
       "      <th>date</th>\n",
       "      <th>time</th>\n",
       "      <th>code</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>万 科Ａ</td>\n",
       "      <td>32.710</td>\n",
       "      <td>32.560</td>\n",
       "      <td>32.050</td>\n",
       "      <td>32.810</td>\n",
       "      <td>31.780</td>\n",
       "      <td>32.040</td>\n",
       "      <td>32.050</td>\n",
       "      <td>80553629</td>\n",
       "      <td>2584309903.290</td>\n",
       "      <td>...</td>\n",
       "      <td>32.060</td>\n",
       "      <td>3005</td>\n",
       "      <td>32.070</td>\n",
       "      <td>119</td>\n",
       "      <td>32.080</td>\n",
       "      <td>344</td>\n",
       "      <td>32.090</td>\n",
       "      <td>2020-01-03</td>\n",
       "      <td>15:00:03</td>\n",
       "      <td>000002</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1 rows × 33 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   name    open pre_close   price    high     low     bid     ask    volume  \\\n",
       "0  万 科Ａ  32.710    32.560  32.050  32.810  31.780  32.040  32.050  80553629   \n",
       "\n",
       "           amount  ...    a2_p  a3_v    a3_p a4_v    a4_p a5_v    a5_p  \\\n",
       "0  2584309903.290  ...  32.060  3005  32.070  119  32.080  344  32.090   \n",
       "\n",
       "         date      time    code  \n",
       "0  2020-01-03  15:00:03  000002  \n",
       "\n",
       "[1 rows x 33 columns]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = ts.get_realtime_quotes('000002') \n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "其运行结果就是当时的股价信息，如果收盘后运行的话获得的就是当日收盘价相关信息。如果觉得列数过多，可以通过DataFrame选取列的方法选取相应的列，代码如下："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>code</th>\n",
       "      <th>name</th>\n",
       "      <th>price</th>\n",
       "      <th>bid</th>\n",
       "      <th>ask</th>\n",
       "      <th>volume</th>\n",
       "      <th>amount</th>\n",
       "      <th>time</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>000002</td>\n",
       "      <td>万 科Ａ</td>\n",
       "      <td>32.050</td>\n",
       "      <td>32.040</td>\n",
       "      <td>32.050</td>\n",
       "      <td>80553629</td>\n",
       "      <td>2584309903.290</td>\n",
       "      <td>15:00:03</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     code  name   price     bid     ask    volume          amount      time\n",
       "0  000002  万 科Ａ  32.050  32.040  32.050  80553629  2584309903.290  15:00:03"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = df[['code','name','price','bid','ask','volume','amount','time']]\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "如果想同时获得多个股票代码的实时数据，可以用如下代码："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>open</th>\n",
       "      <th>pre_close</th>\n",
       "      <th>price</th>\n",
       "      <th>high</th>\n",
       "      <th>low</th>\n",
       "      <th>bid</th>\n",
       "      <th>ask</th>\n",
       "      <th>volume</th>\n",
       "      <th>amount</th>\n",
       "      <th>...</th>\n",
       "      <th>a2_p</th>\n",
       "      <th>a3_v</th>\n",
       "      <th>a3_p</th>\n",
       "      <th>a4_v</th>\n",
       "      <th>a4_p</th>\n",
       "      <th>a5_v</th>\n",
       "      <th>a5_p</th>\n",
       "      <th>date</th>\n",
       "      <th>time</th>\n",
       "      <th>code</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>万 科Ａ</td>\n",
       "      <td>32.710</td>\n",
       "      <td>32.560</td>\n",
       "      <td>32.050</td>\n",
       "      <td>32.810</td>\n",
       "      <td>31.780</td>\n",
       "      <td>32.040</td>\n",
       "      <td>32.050</td>\n",
       "      <td>80553629</td>\n",
       "      <td>2584309903.290</td>\n",
       "      <td>...</td>\n",
       "      <td>32.060</td>\n",
       "      <td>3005</td>\n",
       "      <td>32.070</td>\n",
       "      <td>119</td>\n",
       "      <td>32.080</td>\n",
       "      <td>344</td>\n",
       "      <td>32.090</td>\n",
       "      <td>2020-01-03</td>\n",
       "      <td>15:00:03</td>\n",
       "      <td>000002</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>众泰汽车</td>\n",
       "      <td>3.010</td>\n",
       "      <td>3.000</td>\n",
       "      <td>3.020</td>\n",
       "      <td>3.040</td>\n",
       "      <td>2.970</td>\n",
       "      <td>3.010</td>\n",
       "      <td>3.020</td>\n",
       "      <td>32495074</td>\n",
       "      <td>97566972.190</td>\n",
       "      <td>...</td>\n",
       "      <td>3.030</td>\n",
       "      <td>4849</td>\n",
       "      <td>3.040</td>\n",
       "      <td>3840</td>\n",
       "      <td>3.050</td>\n",
       "      <td>2811</td>\n",
       "      <td>3.060</td>\n",
       "      <td>2020-01-03</td>\n",
       "      <td>15:00:03</td>\n",
       "      <td>000980</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>ST银亿</td>\n",
       "      <td>1.870</td>\n",
       "      <td>1.890</td>\n",
       "      <td>1.810</td>\n",
       "      <td>1.920</td>\n",
       "      <td>1.800</td>\n",
       "      <td>1.810</td>\n",
       "      <td>1.820</td>\n",
       "      <td>40518670</td>\n",
       "      <td>74744476.400</td>\n",
       "      <td>...</td>\n",
       "      <td>1.830</td>\n",
       "      <td>2939</td>\n",
       "      <td>1.840</td>\n",
       "      <td>4163</td>\n",
       "      <td>1.850</td>\n",
       "      <td>1449</td>\n",
       "      <td>1.860</td>\n",
       "      <td>2020-01-03</td>\n",
       "      <td>15:00:03</td>\n",
       "      <td>000981</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>3 rows × 33 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   name    open pre_close   price    high     low     bid     ask    volume  \\\n",
       "0  万 科Ａ  32.710    32.560  32.050  32.810  31.780  32.040  32.050  80553629   \n",
       "1  众泰汽车   3.010     3.000   3.020   3.040   2.970   3.010   3.020  32495074   \n",
       "2  ST银亿   1.870     1.890   1.810   1.920   1.800   1.810   1.820  40518670   \n",
       "\n",
       "           amount  ...    a2_p  a3_v    a3_p  a4_v    a4_p  a5_v    a5_p  \\\n",
       "0  2584309903.290  ...  32.060  3005  32.070   119  32.080   344  32.090   \n",
       "1    97566972.190  ...   3.030  4849   3.040  3840   3.050  2811   3.060   \n",
       "2    74744476.400  ...   1.830  2939   1.840  4163   1.850  1449   1.860   \n",
       "\n",
       "         date      time    code  \n",
       "0  2020-01-03  15:00:03  000002  \n",
       "1  2020-01-03  15:00:03  000980  \n",
       "2  2020-01-03  15:00:03  000981  \n",
       "\n",
       "[3 rows x 33 columns]"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = ts.get_realtime_quotes(['000002','000980','000981'])\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "(4) 获得分笔数据\n",
    "\n",
    "通过如下代码可以获得历史分笔数据，分笔数据也即每笔成交的信息："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "D:\\Anaconda\\Anaconda\\lib\\site-packages\\tushare\\stock\\trading.py:182: FutureWarning: read_table is deprecated, use read_csv instead, passing sep='\\t'.\n",
      "  skiprows=[0])\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>time</th>\n",
       "      <th>price</th>\n",
       "      <th>change</th>\n",
       "      <th>volume</th>\n",
       "      <th>amount</th>\n",
       "      <th>type</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>09:25:04</td>\n",
       "      <td>26.31</td>\n",
       "      <td>0.34</td>\n",
       "      <td>6077</td>\n",
       "      <td>15988903</td>\n",
       "      <td>卖盘</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>09:30:00</td>\n",
       "      <td>26.33</td>\n",
       "      <td>0.02</td>\n",
       "      <td>197</td>\n",
       "      <td>518651</td>\n",
       "      <td>买盘</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>09:30:04</td>\n",
       "      <td>26.33</td>\n",
       "      <td>0.00</td>\n",
       "      <td>4623</td>\n",
       "      <td>12173863</td>\n",
       "      <td>卖盘</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>09:30:06</td>\n",
       "      <td>26.34</td>\n",
       "      <td>0.01</td>\n",
       "      <td>391</td>\n",
       "      <td>1030134</td>\n",
       "      <td>买盘</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>09:30:09</td>\n",
       "      <td>26.35</td>\n",
       "      <td>0.01</td>\n",
       "      <td>3289</td>\n",
       "      <td>8664911</td>\n",
       "      <td>买盘</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       time  price  change  volume    amount type\n",
       "0  09:25:04  26.31    0.34    6077  15988903   卖盘\n",
       "1  09:30:00  26.33    0.02     197    518651   买盘\n",
       "2  09:30:04  26.33    0.00    4623  12173863   卖盘\n",
       "3  09:30:06  26.34    0.01     391   1030134   买盘\n",
       "4  09:30:09  26.35    0.01    3289   8664911   买盘"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = ts.get_tick_data('000002', date='2018-12-12', src='tt')\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "(5) 获得指数信息\n",
    "\n",
    "通过如下代码可以获得上证指数等指数信息："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>code</th>\n",
       "      <th>name</th>\n",
       "      <th>change</th>\n",
       "      <th>open</th>\n",
       "      <th>preclose</th>\n",
       "      <th>close</th>\n",
       "      <th>high</th>\n",
       "      <th>low</th>\n",
       "      <th>volume</th>\n",
       "      <th>amount</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>00上证指数</td>\n",
       "      <td>3089.0220</td>\n",
       "      <td>0.33</td>\n",
       "      <td>3085.1976</td>\n",
       "      <td>3083.7858</td>\n",
       "      <td>3093.8192</td>\n",
       "      <td>3074.5178</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2.899917e+11</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>00Ａ股指数</td>\n",
       "      <td>3236.7077</td>\n",
       "      <td>0.33</td>\n",
       "      <td>3232.6892</td>\n",
       "      <td>3231.1885</td>\n",
       "      <td>3241.7436</td>\n",
       "      <td>3221.4906</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2.899041e+11</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>00Ｂ股指数</td>\n",
       "      <td>261.0510</td>\n",
       "      <td>0.00</td>\n",
       "      <td>261.1236</td>\n",
       "      <td>261.7619</td>\n",
       "      <td>261.7619</td>\n",
       "      <td>260.2429</td>\n",
       "      <td>0.0</td>\n",
       "      <td>8.764934e+07</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>00综合指数</td>\n",
       "      <td>3006.0295</td>\n",
       "      <td>0.39</td>\n",
       "      <td>2999.1744</td>\n",
       "      <td>3006.5318</td>\n",
       "      <td>3018.1699</td>\n",
       "      <td>2998.4266</td>\n",
       "      <td>0.0</td>\n",
       "      <td>6.499701e+10</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>0上证380</td>\n",
       "      <td>4885.0267</td>\n",
       "      <td>0.23</td>\n",
       "      <td>4881.7235</td>\n",
       "      <td>4879.5471</td>\n",
       "      <td>4890.8838</td>\n",
       "      <td>4858.4325</td>\n",
       "      <td>0.0</td>\n",
       "      <td>5.888844e+10</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     code       name  change       open   preclose      close       high  low  \\\n",
       "1  00上证指数  3089.0220    0.33  3085.1976  3083.7858  3093.8192  3074.5178  0.0   \n",
       "2  00Ａ股指数  3236.7077    0.33  3232.6892  3231.1885  3241.7436  3221.4906  0.0   \n",
       "3  00Ｂ股指数   261.0510    0.00   261.1236   261.7619   261.7619   260.2429  0.0   \n",
       "8  00综合指数  3006.0295    0.39  2999.1744  3006.5318  3018.1699  2998.4266  0.0   \n",
       "9  0上证380  4885.0267    0.23  4881.7235  4879.5471  4890.8838  4858.4325  0.0   \n",
       "\n",
       "         volume  amount  \n",
       "1  2.899917e+11     0.0  \n",
       "2  2.899041e+11     0.0  \n",
       "3  8.764934e+07     0.0  \n",
       "8  6.499701e+10     0.0  \n",
       "9  5.888844e+10     0.0  "
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = ts.get_index()\n",
    "df.head()  # 目前的tushare获得的指数的列名有点错乱-2020-01-04备注"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 8.2.2 股票衍生变量生成"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**1.生成股票基本数据**\n",
    "\n",
    "这里首先通过上一节的get_k_data()函数获取从2015-01-01到2019-12-31的股票基本数据："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>date</th>\n",
       "      <th>open</th>\n",
       "      <th>close</th>\n",
       "      <th>high</th>\n",
       "      <th>low</th>\n",
       "      <th>volume</th>\n",
       "      <th>code</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2015-01-05</td>\n",
       "      <td>12.436</td>\n",
       "      <td>12.885</td>\n",
       "      <td>13.214</td>\n",
       "      <td>12.289</td>\n",
       "      <td>6560835.0</td>\n",
       "      <td>000002</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2015-01-06</td>\n",
       "      <td>12.617</td>\n",
       "      <td>12.410</td>\n",
       "      <td>12.954</td>\n",
       "      <td>12.142</td>\n",
       "      <td>3346346.0</td>\n",
       "      <td>000002</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2015-01-07</td>\n",
       "      <td>12.324</td>\n",
       "      <td>12.298</td>\n",
       "      <td>12.531</td>\n",
       "      <td>12.099</td>\n",
       "      <td>2642051.0</td>\n",
       "      <td>000002</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2015-01-08</td>\n",
       "      <td>12.375</td>\n",
       "      <td>11.745</td>\n",
       "      <td>12.419</td>\n",
       "      <td>11.632</td>\n",
       "      <td>2639394.0</td>\n",
       "      <td>000002</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2015-01-09</td>\n",
       "      <td>11.701</td>\n",
       "      <td>11.624</td>\n",
       "      <td>12.289</td>\n",
       "      <td>11.485</td>\n",
       "      <td>3294584.0</td>\n",
       "      <td>000002</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         date    open   close    high     low     volume    code\n",
       "0  2015-01-05  12.436  12.885  13.214  12.289  6560835.0  000002\n",
       "1  2015-01-06  12.617  12.410  12.954  12.142  3346346.0  000002\n",
       "2  2015-01-07  12.324  12.298  12.531  12.099  2642051.0  000002\n",
       "3  2015-01-08  12.375  11.745  12.419  11.632  2639394.0  000002\n",
       "4  2015-01-09  11.701  11.624  12.289  11.485  3294584.0  000002"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = ts.get_k_data('000002',start='2015-01-01',end='2019-12-31')\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>open</th>\n",
       "      <th>close</th>\n",
       "      <th>high</th>\n",
       "      <th>low</th>\n",
       "      <th>volume</th>\n",
       "      <th>code</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2015-01-05</th>\n",
       "      <td>12.436</td>\n",
       "      <td>12.885</td>\n",
       "      <td>13.214</td>\n",
       "      <td>12.289</td>\n",
       "      <td>6560835.0</td>\n",
       "      <td>000002</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-01-06</th>\n",
       "      <td>12.617</td>\n",
       "      <td>12.410</td>\n",
       "      <td>12.954</td>\n",
       "      <td>12.142</td>\n",
       "      <td>3346346.0</td>\n",
       "      <td>000002</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-01-07</th>\n",
       "      <td>12.324</td>\n",
       "      <td>12.298</td>\n",
       "      <td>12.531</td>\n",
       "      <td>12.099</td>\n",
       "      <td>2642051.0</td>\n",
       "      <td>000002</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-01-08</th>\n",
       "      <td>12.375</td>\n",
       "      <td>11.745</td>\n",
       "      <td>12.419</td>\n",
       "      <td>11.632</td>\n",
       "      <td>2639394.0</td>\n",
       "      <td>000002</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-01-09</th>\n",
       "      <td>11.701</td>\n",
       "      <td>11.624</td>\n",
       "      <td>12.289</td>\n",
       "      <td>11.485</td>\n",
       "      <td>3294584.0</td>\n",
       "      <td>000002</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              open   close    high     low     volume    code\n",
       "date                                                         \n",
       "2015-01-05  12.436  12.885  13.214  12.289  6560835.0  000002\n",
       "2015-01-06  12.617  12.410  12.954  12.142  3346346.0  000002\n",
       "2015-01-07  12.324  12.298  12.531  12.099  2642051.0  000002\n",
       "2015-01-08  12.375  11.745  12.419  11.632  2639394.0  000002\n",
       "2015-01-09  11.701  11.624  12.289  11.485  3294584.0  000002"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 通过set_index()函数可以将日期列设置为行索引：\n",
    "df = df.set_index('date')\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**2.简单衍生变量的计算**\n",
    "\n",
    "通过如下代码我们可以先构造一些简单的衍生变量："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>open</th>\n",
       "      <th>close</th>\n",
       "      <th>high</th>\n",
       "      <th>low</th>\n",
       "      <th>volume</th>\n",
       "      <th>code</th>\n",
       "      <th>close-open</th>\n",
       "      <th>high-low</th>\n",
       "      <th>pre_close</th>\n",
       "      <th>price_change</th>\n",
       "      <th>p_change</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2015-01-05</th>\n",
       "      <td>12.436</td>\n",
       "      <td>12.885</td>\n",
       "      <td>13.214</td>\n",
       "      <td>12.289</td>\n",
       "      <td>6560835.0</td>\n",
       "      <td>000002</td>\n",
       "      <td>0.036105</td>\n",
       "      <td>0.075271</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-01-06</th>\n",
       "      <td>12.617</td>\n",
       "      <td>12.410</td>\n",
       "      <td>12.954</td>\n",
       "      <td>12.142</td>\n",
       "      <td>3346346.0</td>\n",
       "      <td>000002</td>\n",
       "      <td>-0.016406</td>\n",
       "      <td>0.066875</td>\n",
       "      <td>12.885</td>\n",
       "      <td>-0.475</td>\n",
       "      <td>-3.686457</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-01-07</th>\n",
       "      <td>12.324</td>\n",
       "      <td>12.298</td>\n",
       "      <td>12.531</td>\n",
       "      <td>12.099</td>\n",
       "      <td>2642051.0</td>\n",
       "      <td>000002</td>\n",
       "      <td>-0.002110</td>\n",
       "      <td>0.035705</td>\n",
       "      <td>12.410</td>\n",
       "      <td>-0.112</td>\n",
       "      <td>-0.902498</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-01-08</th>\n",
       "      <td>12.375</td>\n",
       "      <td>11.745</td>\n",
       "      <td>12.419</td>\n",
       "      <td>11.632</td>\n",
       "      <td>2639394.0</td>\n",
       "      <td>000002</td>\n",
       "      <td>-0.050909</td>\n",
       "      <td>0.067658</td>\n",
       "      <td>12.298</td>\n",
       "      <td>-0.553</td>\n",
       "      <td>-4.496666</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-01-09</th>\n",
       "      <td>11.701</td>\n",
       "      <td>11.624</td>\n",
       "      <td>12.289</td>\n",
       "      <td>11.485</td>\n",
       "      <td>3294584.0</td>\n",
       "      <td>000002</td>\n",
       "      <td>-0.006581</td>\n",
       "      <td>0.070004</td>\n",
       "      <td>11.745</td>\n",
       "      <td>-0.121</td>\n",
       "      <td>-1.030226</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              open   close    high     low     volume    code  close-open  \\\n",
       "date                                                                        \n",
       "2015-01-05  12.436  12.885  13.214  12.289  6560835.0  000002    0.036105   \n",
       "2015-01-06  12.617  12.410  12.954  12.142  3346346.0  000002   -0.016406   \n",
       "2015-01-07  12.324  12.298  12.531  12.099  2642051.0  000002   -0.002110   \n",
       "2015-01-08  12.375  11.745  12.419  11.632  2639394.0  000002   -0.050909   \n",
       "2015-01-09  11.701  11.624  12.289  11.485  3294584.0  000002   -0.006581   \n",
       "\n",
       "            high-low  pre_close  price_change  p_change  \n",
       "date                                                     \n",
       "2015-01-05  0.075271        NaN           NaN       NaN  \n",
       "2015-01-06  0.066875     12.885        -0.475 -3.686457  \n",
       "2015-01-07  0.035705     12.410        -0.112 -0.902498  \n",
       "2015-01-08  0.067658     12.298        -0.553 -4.496666  \n",
       "2015-01-09  0.070004     11.745        -0.121 -1.030226  "
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['close-open'] = (df['close'] - df['open'])/df['open']\n",
    "df['high-low'] = (df['high'] - df['low'])/df['low']\n",
    "\n",
    "df['pre_close'] = df['close'].shift(1)  # 该列所有往下移一行形成昨日收盘价\n",
    "df['price_change'] = df['close']-df['pre_close']\n",
    "df['p_change'] = (df['close']-df['pre_close'])/df['pre_close']*100\n",
    "\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**3.移动平均线指标MA值**\n",
    "\n",
    "通过如下代码可以获得股价的5日移动平均值和10日移动平均值："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>open</th>\n",
       "      <th>close</th>\n",
       "      <th>high</th>\n",
       "      <th>low</th>\n",
       "      <th>volume</th>\n",
       "      <th>code</th>\n",
       "      <th>close-open</th>\n",
       "      <th>high-low</th>\n",
       "      <th>pre_close</th>\n",
       "      <th>price_change</th>\n",
       "      <th>p_change</th>\n",
       "      <th>MA5</th>\n",
       "      <th>MA10</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2015-01-05</th>\n",
       "      <td>12.436</td>\n",
       "      <td>12.885</td>\n",
       "      <td>13.214</td>\n",
       "      <td>12.289</td>\n",
       "      <td>6560835.0</td>\n",
       "      <td>000002</td>\n",
       "      <td>0.036105</td>\n",
       "      <td>0.075271</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-01-06</th>\n",
       "      <td>12.617</td>\n",
       "      <td>12.410</td>\n",
       "      <td>12.954</td>\n",
       "      <td>12.142</td>\n",
       "      <td>3346346.0</td>\n",
       "      <td>000002</td>\n",
       "      <td>-0.016406</td>\n",
       "      <td>0.066875</td>\n",
       "      <td>12.885</td>\n",
       "      <td>-0.475</td>\n",
       "      <td>-3.686457</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-01-07</th>\n",
       "      <td>12.324</td>\n",
       "      <td>12.298</td>\n",
       "      <td>12.531</td>\n",
       "      <td>12.099</td>\n",
       "      <td>2642051.0</td>\n",
       "      <td>000002</td>\n",
       "      <td>-0.002110</td>\n",
       "      <td>0.035705</td>\n",
       "      <td>12.410</td>\n",
       "      <td>-0.112</td>\n",
       "      <td>-0.902498</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-01-08</th>\n",
       "      <td>12.375</td>\n",
       "      <td>11.745</td>\n",
       "      <td>12.419</td>\n",
       "      <td>11.632</td>\n",
       "      <td>2639394.0</td>\n",
       "      <td>000002</td>\n",
       "      <td>-0.050909</td>\n",
       "      <td>0.067658</td>\n",
       "      <td>12.298</td>\n",
       "      <td>-0.553</td>\n",
       "      <td>-4.496666</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-01-09</th>\n",
       "      <td>11.701</td>\n",
       "      <td>11.624</td>\n",
       "      <td>12.289</td>\n",
       "      <td>11.485</td>\n",
       "      <td>3294584.0</td>\n",
       "      <td>000002</td>\n",
       "      <td>-0.006581</td>\n",
       "      <td>0.070004</td>\n",
       "      <td>11.745</td>\n",
       "      <td>-0.121</td>\n",
       "      <td>-1.030226</td>\n",
       "      <td>12.1924</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-01-12</th>\n",
       "      <td>11.511</td>\n",
       "      <td>11.338</td>\n",
       "      <td>11.511</td>\n",
       "      <td>11.019</td>\n",
       "      <td>2436341.0</td>\n",
       "      <td>000002</td>\n",
       "      <td>-0.015029</td>\n",
       "      <td>0.044650</td>\n",
       "      <td>11.624</td>\n",
       "      <td>-0.286</td>\n",
       "      <td>-2.460427</td>\n",
       "      <td>11.8830</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-01-13</th>\n",
       "      <td>11.278</td>\n",
       "      <td>11.295</td>\n",
       "      <td>11.563</td>\n",
       "      <td>11.209</td>\n",
       "      <td>1664610.0</td>\n",
       "      <td>000002</td>\n",
       "      <td>0.001507</td>\n",
       "      <td>0.031582</td>\n",
       "      <td>11.338</td>\n",
       "      <td>-0.043</td>\n",
       "      <td>-0.379256</td>\n",
       "      <td>11.6600</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-01-14</th>\n",
       "      <td>11.295</td>\n",
       "      <td>11.321</td>\n",
       "      <td>11.494</td>\n",
       "      <td>11.122</td>\n",
       "      <td>1646818.0</td>\n",
       "      <td>000002</td>\n",
       "      <td>0.002302</td>\n",
       "      <td>0.033447</td>\n",
       "      <td>11.295</td>\n",
       "      <td>0.026</td>\n",
       "      <td>0.230190</td>\n",
       "      <td>11.4646</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-01-15</th>\n",
       "      <td>11.347</td>\n",
       "      <td>11.900</td>\n",
       "      <td>11.952</td>\n",
       "      <td>11.235</td>\n",
       "      <td>2429686.0</td>\n",
       "      <td>000002</td>\n",
       "      <td>0.048735</td>\n",
       "      <td>0.063818</td>\n",
       "      <td>11.321</td>\n",
       "      <td>0.579</td>\n",
       "      <td>5.114389</td>\n",
       "      <td>11.4956</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-01-16</th>\n",
       "      <td>11.900</td>\n",
       "      <td>11.684</td>\n",
       "      <td>11.900</td>\n",
       "      <td>11.572</td>\n",
       "      <td>2129475.0</td>\n",
       "      <td>000002</td>\n",
       "      <td>-0.018151</td>\n",
       "      <td>0.028344</td>\n",
       "      <td>11.900</td>\n",
       "      <td>-0.216</td>\n",
       "      <td>-1.815126</td>\n",
       "      <td>11.5076</td>\n",
       "      <td>11.8500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-01-19</th>\n",
       "      <td>10.803</td>\n",
       "      <td>10.517</td>\n",
       "      <td>11.148</td>\n",
       "      <td>10.517</td>\n",
       "      <td>3603625.0</td>\n",
       "      <td>000002</td>\n",
       "      <td>-0.026474</td>\n",
       "      <td>0.059998</td>\n",
       "      <td>11.684</td>\n",
       "      <td>-1.167</td>\n",
       "      <td>-9.988018</td>\n",
       "      <td>11.3434</td>\n",
       "      <td>11.6132</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-01-20</th>\n",
       "      <td>10.543</td>\n",
       "      <td>10.673</td>\n",
       "      <td>10.889</td>\n",
       "      <td>10.422</td>\n",
       "      <td>2914688.0</td>\n",
       "      <td>000002</td>\n",
       "      <td>0.012330</td>\n",
       "      <td>0.044809</td>\n",
       "      <td>10.517</td>\n",
       "      <td>0.156</td>\n",
       "      <td>1.483313</td>\n",
       "      <td>11.2190</td>\n",
       "      <td>11.4395</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-01-21</th>\n",
       "      <td>10.656</td>\n",
       "      <td>11.278</td>\n",
       "      <td>11.407</td>\n",
       "      <td>10.457</td>\n",
       "      <td>3555294.0</td>\n",
       "      <td>000002</td>\n",
       "      <td>0.058371</td>\n",
       "      <td>0.090848</td>\n",
       "      <td>10.673</td>\n",
       "      <td>0.605</td>\n",
       "      <td>5.668509</td>\n",
       "      <td>11.2104</td>\n",
       "      <td>11.3375</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-01-22</th>\n",
       "      <td>11.252</td>\n",
       "      <td>11.736</td>\n",
       "      <td>11.796</td>\n",
       "      <td>11.166</td>\n",
       "      <td>3224727.0</td>\n",
       "      <td>000002</td>\n",
       "      <td>0.043015</td>\n",
       "      <td>0.056421</td>\n",
       "      <td>11.278</td>\n",
       "      <td>0.458</td>\n",
       "      <td>4.061004</td>\n",
       "      <td>11.1776</td>\n",
       "      <td>11.3366</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-01-23</th>\n",
       "      <td>11.727</td>\n",
       "      <td>12.030</td>\n",
       "      <td>12.177</td>\n",
       "      <td>11.494</td>\n",
       "      <td>3310408.0</td>\n",
       "      <td>000002</td>\n",
       "      <td>0.025838</td>\n",
       "      <td>0.059422</td>\n",
       "      <td>11.736</td>\n",
       "      <td>0.294</td>\n",
       "      <td>2.505112</td>\n",
       "      <td>11.2468</td>\n",
       "      <td>11.3772</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              open   close    high     low     volume    code  close-open  \\\n",
       "date                                                                        \n",
       "2015-01-05  12.436  12.885  13.214  12.289  6560835.0  000002    0.036105   \n",
       "2015-01-06  12.617  12.410  12.954  12.142  3346346.0  000002   -0.016406   \n",
       "2015-01-07  12.324  12.298  12.531  12.099  2642051.0  000002   -0.002110   \n",
       "2015-01-08  12.375  11.745  12.419  11.632  2639394.0  000002   -0.050909   \n",
       "2015-01-09  11.701  11.624  12.289  11.485  3294584.0  000002   -0.006581   \n",
       "2015-01-12  11.511  11.338  11.511  11.019  2436341.0  000002   -0.015029   \n",
       "2015-01-13  11.278  11.295  11.563  11.209  1664610.0  000002    0.001507   \n",
       "2015-01-14  11.295  11.321  11.494  11.122  1646818.0  000002    0.002302   \n",
       "2015-01-15  11.347  11.900  11.952  11.235  2429686.0  000002    0.048735   \n",
       "2015-01-16  11.900  11.684  11.900  11.572  2129475.0  000002   -0.018151   \n",
       "2015-01-19  10.803  10.517  11.148  10.517  3603625.0  000002   -0.026474   \n",
       "2015-01-20  10.543  10.673  10.889  10.422  2914688.0  000002    0.012330   \n",
       "2015-01-21  10.656  11.278  11.407  10.457  3555294.0  000002    0.058371   \n",
       "2015-01-22  11.252  11.736  11.796  11.166  3224727.0  000002    0.043015   \n",
       "2015-01-23  11.727  12.030  12.177  11.494  3310408.0  000002    0.025838   \n",
       "\n",
       "            high-low  pre_close  price_change  p_change      MA5     MA10  \n",
       "date                                                                       \n",
       "2015-01-05  0.075271        NaN           NaN       NaN      NaN      NaN  \n",
       "2015-01-06  0.066875     12.885        -0.475 -3.686457      NaN      NaN  \n",
       "2015-01-07  0.035705     12.410        -0.112 -0.902498      NaN      NaN  \n",
       "2015-01-08  0.067658     12.298        -0.553 -4.496666      NaN      NaN  \n",
       "2015-01-09  0.070004     11.745        -0.121 -1.030226  12.1924      NaN  \n",
       "2015-01-12  0.044650     11.624        -0.286 -2.460427  11.8830      NaN  \n",
       "2015-01-13  0.031582     11.338        -0.043 -0.379256  11.6600      NaN  \n",
       "2015-01-14  0.033447     11.295         0.026  0.230190  11.4646      NaN  \n",
       "2015-01-15  0.063818     11.321         0.579  5.114389  11.4956      NaN  \n",
       "2015-01-16  0.028344     11.900        -0.216 -1.815126  11.5076  11.8500  \n",
       "2015-01-19  0.059998     11.684        -1.167 -9.988018  11.3434  11.6132  \n",
       "2015-01-20  0.044809     10.517         0.156  1.483313  11.2190  11.4395  \n",
       "2015-01-21  0.090848     10.673         0.605  5.668509  11.2104  11.3375  \n",
       "2015-01-22  0.056421     11.278         0.458  4.061004  11.1776  11.3366  \n",
       "2015-01-23  0.059422     11.736         0.294  2.505112  11.2468  11.3772  "
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['MA5'] = df['close'].rolling(5).mean()\n",
    "df['MA10'] = df['close'].rolling(10).mean()\n",
    "\n",
    "df.head(15)  # head(15)表示展示前15行，因为要展示10行以上，才能看到MA10有值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>open</th>\n",
       "      <th>close</th>\n",
       "      <th>high</th>\n",
       "      <th>low</th>\n",
       "      <th>volume</th>\n",
       "      <th>code</th>\n",
       "      <th>close-open</th>\n",
       "      <th>high-low</th>\n",
       "      <th>pre_close</th>\n",
       "      <th>price_change</th>\n",
       "      <th>p_change</th>\n",
       "      <th>MA5</th>\n",
       "      <th>MA10</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2015-01-16</th>\n",
       "      <td>11.900</td>\n",
       "      <td>11.684</td>\n",
       "      <td>11.900</td>\n",
       "      <td>11.572</td>\n",
       "      <td>2129475.0</td>\n",
       "      <td>000002</td>\n",
       "      <td>-0.018151</td>\n",
       "      <td>0.028344</td>\n",
       "      <td>11.900</td>\n",
       "      <td>-0.216</td>\n",
       "      <td>-1.815126</td>\n",
       "      <td>11.5076</td>\n",
       "      <td>11.8500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-01-19</th>\n",
       "      <td>10.803</td>\n",
       "      <td>10.517</td>\n",
       "      <td>11.148</td>\n",
       "      <td>10.517</td>\n",
       "      <td>3603625.0</td>\n",
       "      <td>000002</td>\n",
       "      <td>-0.026474</td>\n",
       "      <td>0.059998</td>\n",
       "      <td>11.684</td>\n",
       "      <td>-1.167</td>\n",
       "      <td>-9.988018</td>\n",
       "      <td>11.3434</td>\n",
       "      <td>11.6132</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-01-20</th>\n",
       "      <td>10.543</td>\n",
       "      <td>10.673</td>\n",
       "      <td>10.889</td>\n",
       "      <td>10.422</td>\n",
       "      <td>2914688.0</td>\n",
       "      <td>000002</td>\n",
       "      <td>0.012330</td>\n",
       "      <td>0.044809</td>\n",
       "      <td>10.517</td>\n",
       "      <td>0.156</td>\n",
       "      <td>1.483313</td>\n",
       "      <td>11.2190</td>\n",
       "      <td>11.4395</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-01-21</th>\n",
       "      <td>10.656</td>\n",
       "      <td>11.278</td>\n",
       "      <td>11.407</td>\n",
       "      <td>10.457</td>\n",
       "      <td>3555294.0</td>\n",
       "      <td>000002</td>\n",
       "      <td>0.058371</td>\n",
       "      <td>0.090848</td>\n",
       "      <td>10.673</td>\n",
       "      <td>0.605</td>\n",
       "      <td>5.668509</td>\n",
       "      <td>11.2104</td>\n",
       "      <td>11.3375</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-01-22</th>\n",
       "      <td>11.252</td>\n",
       "      <td>11.736</td>\n",
       "      <td>11.796</td>\n",
       "      <td>11.166</td>\n",
       "      <td>3224727.0</td>\n",
       "      <td>000002</td>\n",
       "      <td>0.043015</td>\n",
       "      <td>0.056421</td>\n",
       "      <td>11.278</td>\n",
       "      <td>0.458</td>\n",
       "      <td>4.061004</td>\n",
       "      <td>11.1776</td>\n",
       "      <td>11.3366</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              open   close    high     low     volume    code  close-open  \\\n",
       "date                                                                        \n",
       "2015-01-16  11.900  11.684  11.900  11.572  2129475.0  000002   -0.018151   \n",
       "2015-01-19  10.803  10.517  11.148  10.517  3603625.0  000002   -0.026474   \n",
       "2015-01-20  10.543  10.673  10.889  10.422  2914688.0  000002    0.012330   \n",
       "2015-01-21  10.656  11.278  11.407  10.457  3555294.0  000002    0.058371   \n",
       "2015-01-22  11.252  11.736  11.796  11.166  3224727.0  000002    0.043015   \n",
       "\n",
       "            high-low  pre_close  price_change  p_change      MA5     MA10  \n",
       "date                                                                       \n",
       "2015-01-16  0.028344     11.900        -0.216 -1.815126  11.5076  11.8500  \n",
       "2015-01-19  0.059998     11.684        -1.167 -9.988018  11.3434  11.6132  \n",
       "2015-01-20  0.044809     10.517         0.156  1.483313  11.2190  11.4395  \n",
       "2015-01-21  0.090848     10.673         0.605  5.668509  11.2104  11.3375  \n",
       "2015-01-22  0.056421     11.278         0.458  4.061004  11.1776  11.3366  "
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 删除空值\n",
    "df.dropna(inplace=True)  # 删除空值行，也可以写成df = df.dropna()\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**4.股票衍生变量生成库：TA-Lib库的安装**\n",
    "\n",
    "下面要讲的衍生变量指标都是通过股票衍生变量生成库：TA-Lib库生成的，所以这里我们先讲解一下如何安装Ta-Lib库：\n",
    "\n",
    "以Windows操作系统为例，如果你的系统是Windows的64位系统，直接使用pip install talib语句会报错，原因在于python pip源中TA-Lib是32位的，不能安装在64位系统平台上。\n",
    "\n",
    "正确的方法是下载64位的安装包后本地安装，下载推荐使用加州大学的python扩展库，地址：https://www.lfd.uci.edu/~gohlke/pythonlibs/\n",
    "\n",
    "进入网址后Ctrl + F键搜索“ta_lib”，如下图所示，\n",
    "![图片:]( https://uploader.shimo.im/f/rd7iXLJw6RMZPkbV.png!thumbnail)\n",
    "\n",
    "选择对应的文件TA_Lib-0.4.17-cp37-cp37m-win_amd64.whl（cp后的37表示的是Python3.7版本）下载到自己选择的文件夹，读者在下载时也要根据自己Python的版本进行下载。\n",
    "\n",
    "如何查看自己Python的版本，可以通过Win + R键调出运行框，然后输入cmd，在弹出界面中输入python，然后按一下Enter回车键即可查看相关版本，如下图所示：\n",
    "\n",
    "![图片:]( https://uploader.shimo.im/f/90luFuZqHt46OZko.png)\n",
    "\n",
    "下载完成后，在自己选择的文件夹中（例如笔者保存在的文件夹“E:\\机器学习与大数据分析\\随机森林”），如下图所示，在搜索框中输入cmd后按一下Enter回车键搜索：\n",
    "\n",
    "![图片:]( https://uploader.shimo.im/f/EnabNoMQKT0tYdaz.png!thumbnail)\n",
    "\n",
    "在弹出框中输入如下内容，然后Enter回车键安装即可。\n",
    "\n",
    "pip install TA_Lib-0.4.17-cp37-cp37m-win_amd64.whl"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**5.通过TA-Lib库生成相对强弱指标RSI值**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "import talib\n",
    "df['RSI'] = talib.RSI(df['close'], timeperiod=12)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**6.通过TA-Lib库生成动量指标MOM值**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "df['MOM'] = talib.MOM(df['close'], timeperiod=5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**7.通过TA-Lib库生成指数移动平均值EMA**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "df['EMA12'] = talib.EMA(df['close'], timeperiod=12)  # 12日指数移动平均线\n",
    "df['EMA26'] = talib.EMA(df['close'], timeperiod=26)  # 26日指数移动平均线"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**8.通过TA-Lib库生成异同移动平均线MACD值**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "df['MACD'], df['MACDsignal'], df['MACDhist'] = talib.MACD(df['close'], fastperiod=12, slowperiod=26, signalperiod=9) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>open</th>\n",
       "      <th>close</th>\n",
       "      <th>high</th>\n",
       "      <th>low</th>\n",
       "      <th>volume</th>\n",
       "      <th>code</th>\n",
       "      <th>close-open</th>\n",
       "      <th>high-low</th>\n",
       "      <th>pre_close</th>\n",
       "      <th>price_change</th>\n",
       "      <th>p_change</th>\n",
       "      <th>MA5</th>\n",
       "      <th>MA10</th>\n",
       "      <th>RSI</th>\n",
       "      <th>MOM</th>\n",
       "      <th>EMA12</th>\n",
       "      <th>EMA26</th>\n",
       "      <th>MACD</th>\n",
       "      <th>MACDsignal</th>\n",
       "      <th>MACDhist</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2019-12-25</th>\n",
       "      <td>30.40</td>\n",
       "      <td>30.29</td>\n",
       "      <td>30.63</td>\n",
       "      <td>30.18</td>\n",
       "      <td>685037.0</td>\n",
       "      <td>000002</td>\n",
       "      <td>-0.003618</td>\n",
       "      <td>0.014911</td>\n",
       "      <td>30.38</td>\n",
       "      <td>-0.09</td>\n",
       "      <td>-0.296248</td>\n",
       "      <td>30.878</td>\n",
       "      <td>30.075</td>\n",
       "      <td>63.075563</td>\n",
       "      <td>-0.02</td>\n",
       "      <td>29.908556</td>\n",
       "      <td>28.973211</td>\n",
       "      <td>0.935345</td>\n",
       "      <td>0.772958</td>\n",
       "      <td>0.162387</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-12-26</th>\n",
       "      <td>30.50</td>\n",
       "      <td>31.12</td>\n",
       "      <td>31.30</td>\n",
       "      <td>30.50</td>\n",
       "      <td>888790.0</td>\n",
       "      <td>000002</td>\n",
       "      <td>0.020328</td>\n",
       "      <td>0.026230</td>\n",
       "      <td>30.29</td>\n",
       "      <td>0.83</td>\n",
       "      <td>2.740178</td>\n",
       "      <td>30.896</td>\n",
       "      <td>30.387</td>\n",
       "      <td>68.890164</td>\n",
       "      <td>0.09</td>\n",
       "      <td>30.094932</td>\n",
       "      <td>29.132233</td>\n",
       "      <td>0.962699</td>\n",
       "      <td>0.810906</td>\n",
       "      <td>0.151793</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-12-27</th>\n",
       "      <td>31.23</td>\n",
       "      <td>31.00</td>\n",
       "      <td>31.32</td>\n",
       "      <td>30.81</td>\n",
       "      <td>703096.0</td>\n",
       "      <td>000002</td>\n",
       "      <td>-0.007365</td>\n",
       "      <td>0.016553</td>\n",
       "      <td>31.12</td>\n",
       "      <td>-0.12</td>\n",
       "      <td>-0.385604</td>\n",
       "      <td>30.760</td>\n",
       "      <td>30.672</td>\n",
       "      <td>67.220611</td>\n",
       "      <td>-0.68</td>\n",
       "      <td>30.234173</td>\n",
       "      <td>29.270586</td>\n",
       "      <td>0.963587</td>\n",
       "      <td>0.841442</td>\n",
       "      <td>0.122145</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-12-30</th>\n",
       "      <td>31.35</td>\n",
       "      <td>31.57</td>\n",
       "      <td>31.79</td>\n",
       "      <td>31.02</td>\n",
       "      <td>915751.0</td>\n",
       "      <td>000002</td>\n",
       "      <td>0.007018</td>\n",
       "      <td>0.024823</td>\n",
       "      <td>31.00</td>\n",
       "      <td>0.57</td>\n",
       "      <td>1.838710</td>\n",
       "      <td>30.872</td>\n",
       "      <td>30.884</td>\n",
       "      <td>70.877814</td>\n",
       "      <td>0.56</td>\n",
       "      <td>30.439685</td>\n",
       "      <td>29.440913</td>\n",
       "      <td>0.998772</td>\n",
       "      <td>0.872908</td>\n",
       "      <td>0.125864</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-12-31</th>\n",
       "      <td>31.35</td>\n",
       "      <td>32.18</td>\n",
       "      <td>32.45</td>\n",
       "      <td>31.32</td>\n",
       "      <td>663497.0</td>\n",
       "      <td>000002</td>\n",
       "      <td>0.026475</td>\n",
       "      <td>0.036079</td>\n",
       "      <td>31.57</td>\n",
       "      <td>0.61</td>\n",
       "      <td>1.932214</td>\n",
       "      <td>31.232</td>\n",
       "      <td>31.057</td>\n",
       "      <td>74.233951</td>\n",
       "      <td>1.80</td>\n",
       "      <td>30.707426</td>\n",
       "      <td>29.643808</td>\n",
       "      <td>1.063618</td>\n",
       "      <td>0.911050</td>\n",
       "      <td>0.152567</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "             open  close   high    low    volume    code  close-open  \\\n",
       "date                                                                   \n",
       "2019-12-25  30.40  30.29  30.63  30.18  685037.0  000002   -0.003618   \n",
       "2019-12-26  30.50  31.12  31.30  30.50  888790.0  000002    0.020328   \n",
       "2019-12-27  31.23  31.00  31.32  30.81  703096.0  000002   -0.007365   \n",
       "2019-12-30  31.35  31.57  31.79  31.02  915751.0  000002    0.007018   \n",
       "2019-12-31  31.35  32.18  32.45  31.32  663497.0  000002    0.026475   \n",
       "\n",
       "            high-low  pre_close  price_change  p_change     MA5    MA10  \\\n",
       "date                                                                      \n",
       "2019-12-25  0.014911      30.38         -0.09 -0.296248  30.878  30.075   \n",
       "2019-12-26  0.026230      30.29          0.83  2.740178  30.896  30.387   \n",
       "2019-12-27  0.016553      31.12         -0.12 -0.385604  30.760  30.672   \n",
       "2019-12-30  0.024823      31.00          0.57  1.838710  30.872  30.884   \n",
       "2019-12-31  0.036079      31.57          0.61  1.932214  31.232  31.057   \n",
       "\n",
       "                  RSI   MOM      EMA12      EMA26      MACD  MACDsignal  \\\n",
       "date                                                                      \n",
       "2019-12-25  63.075563 -0.02  29.908556  28.973211  0.935345    0.772958   \n",
       "2019-12-26  68.890164  0.09  30.094932  29.132233  0.962699    0.810906   \n",
       "2019-12-27  67.220611 -0.68  30.234173  29.270586  0.963587    0.841442   \n",
       "2019-12-30  70.877814  0.56  30.439685  29.440913  0.998772    0.872908   \n",
       "2019-12-31  74.233951  1.80  30.707426  29.643808  1.063618    0.911050   \n",
       "\n",
       "            MACDhist  \n",
       "date                  \n",
       "2019-12-25  0.162387  \n",
       "2019-12-26  0.151793  \n",
       "2019-12-27  0.122145  \n",
       "2019-12-30  0.125864  \n",
       "2019-12-31  0.152567  "
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.dropna(inplace=True)  # 删除空行\n",
    "df.tail()  # 和head()相对，通过tail()函数可以查看后五行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 补充内容：Talib库的一些验证"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "RSI指标的验证"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>close</th>\n",
       "      <th>RSI</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>10</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>12</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>11</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>13</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>12</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>14</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>13</td>\n",
       "      <td>66.666667</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   close        RSI\n",
       "0     10        NaN\n",
       "1     12        NaN\n",
       "2     11        NaN\n",
       "3     13        NaN\n",
       "4     12        NaN\n",
       "5     14        NaN\n",
       "6     13  66.666667"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import talib\n",
    "\n",
    "data = pd.DataFrame()\n",
    "data['close'] = [10, 12, 11, 13, 12, 14, 13]\n",
    "data['RSI'] = talib.RSI(data['close'], timeperiod=6)\n",
    "\n",
    "data"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
